package org.jeecg.modules.strategy.component;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ScriptException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.AbstractDownloader;
import us.codecraft.webmagic.selector.PlainText;

import java.net.MalformedURLException;
import java.net.URL;

@Component
public class MetaDownloader extends AbstractDownloader {
    private static final Logger logger = LoggerFactory.getLogger(MetaDownloader.class);
    @Override
    public Page download(Request request, Task task) {
        try (WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
            String url = request.getUrl();

            webClient.getOptions().setUseInsecureSSL(true);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setPrintContentOnFailingStatusCode(false);

            webClient.getOptions().setJavaScriptEnabled(true);
            webClient.getOptions().setCssEnabled(false);
            webClient.getOptions().setDownloadImages(false);
            webClient.getCache().setMaxSize(0);
            webClient.setJavaScriptTimeout(15000);

            webClient.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            webClient.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
            webClient.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
            webClient.addRequestHeader("Referer", url);

            webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {
                @Override
                public void scriptException(HtmlPage page, ScriptException scriptException) {
                    logger.warn("JavaScript执行错误: {}", scriptException.getMessage());
                }

                @Override
                public void timeoutError(HtmlPage page, long allowedTime, long executionTime) {
                    logger.warn("JavaScript执行超时");
                }

                @Override
                public void warn(String s, String s1, int i, String s2, int i1) {
                }

                @Override
                public void malformedScriptURL(HtmlPage page, String url, MalformedURLException malformedURLException) {
                    logger.warn("错误的脚本URL: {}", url);
                }

                @Override
                public void loadScriptError(HtmlPage page, URL scriptUrl, Exception exception) {
                    logger.warn("脚本加载错误: {}", scriptUrl);
                }
            });

            HtmlPage htmlPage = webClient.getPage(url);
            webClient.waitForBackgroundJavaScript(5000);
            String html = htmlPage.asXml();

            Page page = new Page();
            page.setRawText(html);
            page.setUrl(new PlainText(url));
            page.setRequest(request);
            return page;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void setThread(int i) {

    }
}
